package com.hzhc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.YwOrderCntr;
import com.hzhc.entity.YwOrderCntrAudit;
import com.hzhc.mapper.YwOrderCntrAuditMapper;
import com.hzhc.mapper.YwOrderCntrMapper;
import com.hzhc.service.SysMsgNotifyService;
import com.hzhc.entity.*;
import com.hzhc.mapper.*;
import com.hzhc.service.CntrLogService;
import com.hzhc.service.TrainPlanDetailService;
import com.hzhc.service.YwOrderCntrAuditService;
import com.hzhc.service.YwOrderCntrService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.StringUtils;
import com.hzhc.vo.UserMemberVo;
import com.hzhc.vo.YwOrderCntrAuditVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 箱修改申请 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-08-30
 */
@Service
@RequiredArgsConstructor
public class YwOrderCntrAuditServiceImpl extends ServiceImpl<YwOrderCntrAuditMapper, YwOrderCntrAudit> implements YwOrderCntrAuditService {
    private final YwOrderCntrMapper ywOrderCntrMapper;
    private final YwOrderCntrService ywOrderCntrService;
    private final TrainPlanDetailMapper detailMapper;
    private final TrainPlanDetailService detailService;
    private final CntrInfoMapper cntrInfoMapper;
    private final CntrLogService cntrLogService;
    private final VehicleReservationMapper vehicleReservationMapper;
    private final SysMsgNotifyService msgNotifyService;
    private final UserMemberMapper memberMapper;
    @Override
    public List<YwOrderCntrAuditVo> getCntrAuditList(Page<YwOrderCntrAuditVo> page, YwOrderCntrAuditVo vo) {
        IPage<YwOrderCntrAuditVo> iPage = baseMapper.getCntrAuditList(page,vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addCntrAuditRet(YwOrderCntrAuditVo vo) {
        List<YwOrderCntrAudit> list=new ArrayList<>();
        List<YwOrderCntr> cntrList=new ArrayList<>();
        if(vo.getCntrs().size()>0){
            for (YwOrderCntr ywOrderCntr : vo.getCntrs()) {
                YwOrderCntrAudit ywOrderCntrAudit=new YwOrderCntrAudit();
                ywOrderCntrAudit.setCntrId(ywOrderCntr.getId());
                ywOrderCntrAudit.setOrderNo(ywOrderCntr.getOrderNo());
                ywOrderCntrAudit.setCntrNo(ywOrderCntr.getCntrNo());
                ywOrderCntrAudit.setType(vo.getType());
                ywOrderCntrAudit.setUserId(vo.getUserId());
                ywOrderCntrAudit.setCreateTime(vo.getCreateTime());
                list.add(ywOrderCntrAudit);
                ywOrderCntr.setEditFlag(1);
                cntrList.add(ywOrderCntr);
            }
        }
        boolean b = saveBatch(list);
        ywOrderCntrService.updateBatchById(cntrList);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateCntrAuditRet(YwOrderCntrAuditVo vo) {
        YwOrderCntrAudit ywOrderCntrAudit = baseMapper.selectById(vo.getId());
        ywOrderCntrAudit.setAuditFlag(vo.getAuditFlag());
        ywOrderCntrAudit.setAuditTime(vo.getUpdateTime());
        ywOrderCntrAudit.setAuditUserId(vo.getUpdateUserId());
        if(StringUtils.isNotEmpty(vo.getRefugeeReason())){
            ywOrderCntrAudit.setRefugeeReason(vo.getRefugeeReason());
        }
        YwOrderCntr ywOrderCntr = ywOrderCntrMapper.selectById(ywOrderCntrAudit.getCntrId());
        if(vo.getAuditFlag()==1){
            if(ywOrderCntr!=null){
                if("person".equals(ywOrderCntrAudit.getType())){
                    ywOrderCntr.setEditFlag(2);
                }else{
                    ywOrderCntr.setConsignee(vo.getConsignee());
                    ywOrderCntr.setGoodsName(vo.getGoodsName());

                    if(!(vo.getDestination().substring(vo.getDestination().length() - 1).equals("站"))){
                        ywOrderCntr.setDestination(vo.getDestination()+"站");
                    }else{
                        ywOrderCntr.setDestination(vo.getDestination());
                    }
                    ywOrderCntr.setRefugeeReason("");
                    ywOrderCntr.setEditFlag(0);

                    //修改 箱清单 装卸车清单 箱信息 箱动态 车辆预约
                    List<TrainPlanDetail> details = detailMapper.selectList(new QueryWrapper<TrainPlanDetail>().eq("disable", 0)
                            .eq("order_no", ywOrderCntr.getOrderNo()).eq("cntr_no", ywOrderCntr.getCntrNo()));
                    if(details.size()>0){
                        for (TrainPlanDetail detail : details) {
                            detail.setGoods(ywOrderCntr.getGoodsName());
                            detail.setMdPort(ywOrderCntr.getDestination());
                            detail.setConsignee(ywOrderCntr.getConsignee());
                        }
                        detailService.updateBatchById(details);
                    }


                    CntrInfo cntrInfo = cntrInfoMapper.selectById(ywOrderCntr.getCntrNo());
                    if(cntrInfo!=null){
                        cntrInfo.setGoods(ywOrderCntr.getGoodsName());
                        cntrInfo.setMdPort(ywOrderCntr.getDestination());
                        cntrInfo.setEndPort(ywOrderCntr.getDestination());
                        cntrInfo.setConsignee(ywOrderCntr.getConsignee());
                        cntrInfoMapper.updateById(cntrInfo);
                    }

                    List<CntrLog> cntrLogs = cntrLogService.getBaseMapper().selectList(new QueryWrapper<CntrLog>().eq("disable", 0)
                            .eq("cntr_no", ywOrderCntr.getCntrNo()).eq("order_no", ywOrderCntr.getOrderNo()));
                    if(cntrLogs.size()>0){
                        for (CntrLog cntrLog : cntrLogs) {
                            cntrLog.setGoods(ywOrderCntr.getGoodsName());
                            cntrLog.setMdPort(ywOrderCntr.getDestination());
                            cntrLog.setEndPort(ywOrderCntr.getDestination());
                            cntrLog.setConsignee(ywOrderCntr.getConsignee());
                        }
                        cntrLogService.updateBatchById(cntrLogs);
                    }
                }
            }
        }else if(vo.getAuditFlag()==2){
            if(ywOrderCntr!=null){
                ywOrderCntr.setRefugeeReason(vo.getRefugeeReason());
                ywOrderCntr.setEditFlag(0);
            }
        }
        boolean b = updateById(ywOrderCntrAudit);
        if(ywOrderCntr!=null){
            ywOrderCntrMapper.updateById(ywOrderCntr);
        }
        //场站 箱信息审核--消息提醒
        //上传箱子 公司 258 下的员工
        List<UserMemberVo> users = memberMapper.selectAllUser(vo);
        msgNotifyService.saveCntrEditMsg(vo,users);

        return b ? ResultUtil.ok() : ResultUtil.fail();
    }
}
